﻿using System;
using System.ComponentModel.DataAnnotations;

namespace HorseRaceTracker.Domain.Core
{
    public class Horse : IValidatableObject
    {
        [Required]
        public Guid HorseIdentifier { get; private set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public Gender Sex { get; set; }
        [Required]
        public Country BredIn { get; set; }
        [Required]
        public int BirthYear { get; set; }
        [Required]
        public Trainer Trainer { get; set; }

        public Horse()
            : this(Guid.NewGuid())
        {
          
        }
        public Horse(Guid horseIdentifier)
        {
            HorseIdentifier = horseIdentifier;
        }

        public System.Collections.Generic.IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            var currentYear = DateTime.Now.Year;
            var minYear = currentYear - 15;
            var maxYear = currentYear - 1;
            if (BirthYear == 0)
            {
                yield return new ValidationResult("You must specify a year ", new[] { "BirthYear" });
            }
            if (BirthYear < minYear || BirthYear > maxYear)
            {
                yield return new ValidationResult("You must specify a year between  " + minYear.ToString() + " and " + maxYear.ToString(), new[] { "BirthYear" });
            }
            if (Sex== Gender.Unknown)
            {
                yield return new ValidationResult("You must specify a gender ", new[] { "Sex" });
            }
        }

    }

}
